package com.mqtt.nettymqtt.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.google.common.collect.Lists;
import com.mqtt.nettymqtt.entity.Mqtt;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/4
 */
public class JdbcUtil {

    private static final DataSource datasource;

    /**
     * 静态代码块的特点：
     * 1.类加载时：自动执行静态代码块
     * 2.静态代码块仅被执行一次，因为类只会被虚拟机加载一次
     */
    static {
        try {
            Properties prop = new Properties();
            InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("application.yml");
            prop.load(inputStream);
//        创建数据库连接池
            datasource = DruidDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            throw new RuntimeException("连接池初始化失败!");
        }
    }

    //    获取数据库连接池对象
    public static DataSource getDatasource() {
        return datasource;
    }

    //    获取连接
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = datasource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //    释放资源
    public static void release(Connection conn, Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public static void save(Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            String sql = "insert into mqtt(id,message) values(?,?)";
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            preparedStatement.execute();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
//            关闭资源
            release(connection, preparedStatement);
        }
    }

    public static List<Mqtt> select(Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ArrayList<Mqtt> list = Lists.newArrayList();
        try {
            String sql = "select * from mqtt";
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Mqtt mqtt = new Mqtt();
                mqtt.setId(resultSet.getString("id"));
                mqtt.setMessage(resultSet.getString("message"));
                list.add(mqtt);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}
